class ToolLogosController < ApplicationController
  layout 'base.html',:only => [:new, :edit, :show,:create]
  caches_page :show

  def new
    @tool = Tool.find(params[:tool_id])
    @logo = ToolLogo.new( :tool_id => @tool.id)
    @seo_title = "麦吐司网-#{@tool.name}-设置新图标"
  end

  def create
    @logo = ToolLogo.new(params[:tool_logo].merge(:user_id => current_user_id))
    @logo.save ? redirect_to(:action => :edit, :id => @logo.id) : render(:new)
  end

  def edit
    @logo = ToolLogo.find(params[:id])
    @tool = @logo.tool
    @seo_title = "麦吐司网-#{@tool.name}-图标裁剪"
  end

  def update
    @source = ToolLogo.find(params[:id])
    logger.debug "==========http://l.mytusi.com#{tool_logo_path(@source,:jpg)}"
    @logo = ToolLogo.new(
      :user_id => current_user_id,
      :tool_id => @source.tool_id,
      :image_file_url => (Rails.env.production? ? "http://l.mytusi.com#{tool_logo_path(@source,:jpg)}" : tool_logo_url(@source,:jpg))
    )
    
    instance_eval( "
    @logo.operate do |image|
      image.crop(
        :from => '#{params[:x1]}x#{params[:y1]}',
        :size => '#{params[:width]}x#{params[:height]}'
      )
    end ")
    @logo.save ? redirect_to(tool_url @logo.tool) : render(:edit)
  end

  def show
    @logo = ToolLogo.find(params[:id])
  end
end
